﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using XCI.Windows.Forms;
using XCI.Windows.Helper;
using System.Windows.Forms;
using XCI.Windows;
using System.Collections.Generic;
using XCI.Data.Entity;

namespace XCI.Sys.Forms
{
    public partial class FrmWidgetItemEdit : FrmBase
    {
        private WidgetManage widgetManage;
        private WidgetItem model = new WidgetItem();
        private readonly AsyncWorker providerAsyncWorker = new AsyncWorker();

        public FrmWidgetItemEdit(WidgetManage widgetManage)
        {
            InitializeComponent();
            this.widgetManage = widgetManage;
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keys)
        {
            return OnEsc(msg, keys, () => { EditFormEscResultCancel(); });
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected void InitForm()
        {
            providerAsyncWorker
                .RunCallback(e => { e.Result = WinHelper.GetWidgetProvider(); })
                .CompletCallback(e => { BindProvider((List<WidgetCaptionAttribute>)e.Result); })
                .RunAsync();

            BindModel();
        }

        private void BindProvider(List<WidgetCaptionAttribute> data)
        {
            editWidgetProvider.Properties.NullValuePrompt = "正在加载...";
            editWidgetProvider.Metadata = EntityHelper.Get<WidgetCaptionAttribute>();
            editWidgetProvider.DataSource = data;
            editWidgetProvider.Properties.NullValuePrompt = "";
        }

        /// <summary>
        /// 绑定数据到界面
        /// </summary>
        private void BindModel()
        {
            WinHelper.SetFormData(layoutPanel, model);
        }

        /// <summary>
        /// 保存数据对象
        /// </summary>
        private bool SaveModel()
        {
            WinHelper.GetFormData(layoutPanel, model);
            var isValid = ValidModel(model);
            if (!isValid) return false;


            return widgetManage.AddItem(model);
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        private void FrmWidgetItemEdit_Load(object sender, EventArgs e)
        {
            InitForm();
        }

        private void EditWidgetProvider_SelectedValueChanged(object sender, EventArgs e)
        {
            model.Name = string.Empty;
            if (editWidgetProvider.SelectedObject != null)
            {
                model.Name = ((WidgetCaptionAttribute)editWidgetProvider.SelectedObject).Name;
            }
        }

        /// <summary>
        /// 保存事件
        /// </summary>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!SaveModel()) return;
            if (ckContinue.Checked) //连续保存
            {
                MessageBoxHelper.ShowMessage("保存成功");
                model = new WidgetItem();
                BindModel();
            }
            else
            {
                ResultOk();
            }
        }

        /// <summary>
        /// 关闭事件
        /// </summary>
        private void btnClose_Click(object sender, EventArgs e)
        {
            ResultOk();
        }

    }
}